#include <buola/scene/techniques/cmeshtitechnique.h>
#include <buola/scene/crendercontext.h>

#include "shaders/sVertexShader.h"
#include "shaders/sTransformIndexFragment.h"

namespace buola { namespace scene {

CMeshTITechnique::CMeshTITechnique()
{
    mProgram.CreateAndAttach(shaders::sVertexShader,shaders::sTransformIndexFragment);
    mProgram.BindFragmentData(0,"oFragColor");
    mProgram.BindFragmentData(1,"oTransformIndex");
    mProgram.BindAttribLocation(0,"iVertex");
    mProgram.BindAttribLocation(1,"iColor");
    mProgram.BindAttribLocation(2,"iNormal");
    mProgram.Link();

    uMVMatrix=mProgram.GetUniform("uMVMatrix");
    uMVPMatrix=mProgram.GetUniform("uMVPMatrix");
    uTransformIndex=mProgram.GetUniform("uTransformIndex");
}

CMeshTITechnique::~CMeshTITechnique()
{
    
}

void CMeshTITechnique::Prepare(CRenderContext &pContext)
{
    if(pContext.mTechnique!=this)
    {
        mProgram.Use();
        glVertexAttrib3d(1,0.5,0.3,0.7);

        pContext.mTechnique=this;
    }

    mat::CMat34d lMVMatrix=pContext.mViewMatrix*pContext.mModelMatrix;
    uMVMatrix.SetValue(lMVMatrix);
    uMVPMatrix.SetValue(pContext.mProjectionMatrix*lMVMatrix);
    uTransformIndex.SetValue((int)pContext.mTransforms.size()-1);
}

CMeshTITechnique *CMeshTITechnique::Get()
{
    static CMeshTITechnique *lTechnique=new CMeshTITechnique;
    return lTechnique;
}

/*namespace scene*/ } /*namespace buola*/ }
